//
//  JHSDetailDoctorViewController.m
//  JHSSweep
//
//  Created by Apple on 2017/3/24.
//  Copyright © 2017年 JHDS. All rights reserved.
//

#import "JHSDetailDoctorViewController.h"
#import "JHSCureConditionModel.h"
#import "JHSDocIntrodunceModel.h"

@interface JHSDetailDoctorViewController ()<UIScrollViewDelegate>

@property(strong, nonatomic)UIView *indicator;

@property(strong,nonatomic)NSMutableArray<UIView *> *viewArrs;

/**
 医生接诊条件模型数组
 */@property(strong,nonatomic)NSArray<JHSCureConditionModel *> *conditions;
@property (strong,nonatomic) JHSCureConditionModel *cureModel;

@property (strong,nonatomic) JHSDocIntrodunceModel *introdModel;


/**
 头像
 */
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;

/**
 姓名
 */
@property (weak, nonatomic) IBOutlet UILabel *name;

/**
 职位
 */
@property (weak, nonatomic) IBOutlet UILabel *position;

/**
 工作地点
 */
@property (weak, nonatomic) IBOutlet UILabel *workPlace;

/**
 左边label
 */
@property (weak, nonatomic) IBOutlet UILabel *leftLabel;

/**
 右边label
 */
@property (weak, nonatomic) IBOutlet UILabel *rightLabel;

/**
 两个label的底部view
 */
@property (weak, nonatomic) IBOutlet UIView *labelBgView;

@property (weak, nonatomic) IBOutlet UILabel *reservation;

@property (weak, nonatomic) IBOutlet UILabel *flowers;
@property (weak, nonatomic) IBOutlet UILabel *banner;

/**
 锦旗
 */
@property (weak, nonatomic) IBOutlet UIButton *accuracy;

/**
 下方滚动视图
 */
@property (weak, nonatomic) IBOutlet UIScrollView *scrollContentView;

@property(weak,nonatomic)UILabel *coditionLabel;
@property(weak,nonatomic)UILabel *introduceLabel;
@property(assign,nonatomic)BOOL isSelect;


@end

@implementation JHSDetailDoctorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self setupUI];
    [self loadUI];
    [self loadDetail];
}

-(void) loadUI{
    
    self.name.text=_docModel.doctor_name;
    self.position.text=_docModel.doctor_title_name;
    self.workPlace.text=_docModel.hospital_name;
    self.reservation.text = _docModel.operation_count;
    self.flowers.text = _docModel.flower;
    self.banner.text = _docModel.banner;

}

-(void) loadDetail{

//    self.docModel
    NSLog(@"%@------",_docModel);
    NSLog(@"%zd++++",_docModel.doctor_id);
    
    NSInteger doc_id=[_docModel.doctor_id integerValue];
    
     //获取医生接诊条件数据
     NSString *conditionURL = @"http:iosapi.itcast.cn/doctor/doctorReceivingSetting.json.php";
     NSDictionary *conditionParameter = @{@"doctor_id":@(doc_id)};
     
     [JHSCureConditionModel cureDoctorNetDataWithURL:conditionURL withParameters:conditionParameter withSuccessBlock:^(id responceBoby) {
        
//        _conditions =  responceBoby;
//         NSLog(@"%@",_conditions);
         JHSCureConditionModel *model=[JHSCureConditionModel yy_modelWithJSON:responceBoby];
         
         _cureModel = model;
         self.coditionLabel.text=model.receiving_setting_extra;
    } withFailureBlock:^(NSError *error) {
        
        
    }];
    
    //获取医生简介数据
    NSString *introURL = @"http:iosapi.itcast.cn/doctor/getIntroduction.json.php";
    NSDictionary *introsParameter = @{@"doctor_id":@300000315};
    [JHSDocIntrodunceModel introdunceDoctorNetDataWithURL:introURL withParameters:introsParameter withSuccessBlock:^(id responceBoby) {
      
        JHSDocIntrodunceModel *model = [JHSDocIntrodunceModel yy_modelWithJSON:responceBoby];
        
        _introdModel = model;
        
        self.introduceLabel.text = model.introduction;
      
    } withFailureBlock:^(NSError *error) {
        
        
    }];
}

-(void)setupUI{
    
    self.title = @"医生详情";
    self.navigationController.navigationItem.title = @"返回";
    
    self.isSelect = NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"saveTemplateEmpty"] style:UIBarButtonItemStylePlain target:self action:@selector(clickNavRightBarBtn:)];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.labelBgView.backgroundColor = JHSMainColor;
    self.viewArrs = [NSMutableArray arrayWithCapacity:2];
    self.scrollContentView.delegate = self;
    
    //设置头像为圆形
    self.iconImageView.layer.masksToBounds = YES;
    self.iconImageView.layer.cornerRadius = self.iconImageView.bounds.size.width*0.5;
    self.iconImageView.layer.borderWidth = 3;
    self.iconImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    
    //设置中间滚动条
    UIView *v = [[UIView alloc]init];
    v.backgroundColor = JHSMainColor;
    [self.view addSubview:v];
    [v mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(self.leftLabel.mas_bottom);
        make.height.mas_equalTo(3);
        make.width.equalTo(self.leftLabel);
    }];
    
    self.indicator = v;
    
    //设置label
    self.leftLabel.textColor = JHSMainColor;
    self.leftLabel.tag = 1;
    self.leftLabel.userInteractionEnabled = YES;
    
    self.rightLabel.textColor =JHSMainColor;
    self.rightLabel.tag = 2;
    self.rightLabel.userInteractionEnabled = YES;
   
    /*------------* 创建手势 *-------------*/
    UITapGestureRecognizer *tapLeft = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickLabel:)];
    [self.leftLabel addGestureRecognizer:tapLeft];
    
    UITapGestureRecognizer *tapRight = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickLabel:)];
    [self.rightLabel addGestureRecognizer:tapRight];
    
    /*------------* 设置scrollview相关属性 *-------------*/
    self.scrollContentView.showsVerticalScrollIndicator = NO;
    self.scrollContentView.showsHorizontalScrollIndicator= NO;
    self.scrollContentView.pagingEnabled = YES;
    self.scrollContentView.bounces = NO;
    [self.scrollContentView setBackgroundColor:[UIColor whiteColor]];
    
    //设置底部视图
    [self setFooterUI];

}
-(void)clickNavRightBarBtn:(UIBarButtonItem *)sender{
    if (!self.isSelect) {
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"saveTemplate"]];
        [JHSDetailDoctorViewController jhsShowSVSuccessTitle:@"成功关注" andTime:0.8];
    }else{
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"saveTemplateEmpty"]];
        [JHSDetailDoctorViewController jhsShowSVSuccessTitle:@"取消关注" andTime:0.8];
    }
    self.isSelect = ! self.isSelect;
}

#pragma mark  - setter方法

-(void)setDetailModel:(JHSDoctorListModel *)detailModel{
    _detailModel = detailModel;
    self.name.text = _detailModel.doctor_name;
    self.position.text = _detailModel.doctor_title_name;
    //self.workPlace.text = _detailModel.doctor_hospital_name;
    if (detailModel.hospital_name) {
        self.workPlace.text = detailModel.hospital_name;
    }else{
        self.workPlace.text = detailModel.doctor_hospital_name;
    }
    [self.accuracy setTitle:_detailModel.accuracy forState:UIControlStateNormal];
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:_detailModel.doctor_portrait] placeholderImage:[UIImage imageNamed:@"doctor_defaultphoto_male"]];
}


#pragma mark - 手势相关

-(void)clickLabel:(UITapGestureRecognizer *)sender{
    if (sender.view.tag == 1) {
        [self.scrollContentView setContentOffset:CGPointMake(0, 0) animated:YES];
        // 开始动画
        [UIView beginAnimations:nil context:nil];
        // 动画时长
        [UIView setAnimationDuration:2.0];
        [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view);
            make.top.equalTo(self.leftLabel.mas_bottom);
        }];
        // 提交动画
        [UIView commitAnimations];
    }
    else if(sender.view.tag == 2){
        [self.scrollContentView setContentOffset:CGPointMake(self.view.bounds.size.width, 0) animated:YES];
        [UIView animateWithDuration:0.6 animations:^{
            [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.view).mas_offset(self.view.bounds.size.width/2);
                make.top.equalTo(self.rightLabel.mas_bottom);
            }];
        }];
    }
}

-(void)setFooterUI{
    //定义一个宽
    CGFloat width = self.view.bounds.size.width;
    //定义一个高
    CGFloat height = self.view.bounds.size.height;
    //定义个数
    int index = 2;
    for (int i = 0; i < index; i++) {
        UIView *footView = [[UIView alloc]init];
        //背景色
        footView.backgroundColor = [UIColor whiteColor];
        [self.scrollContentView addSubview:footView];
        [self.viewArrs addObject:footView];
        footView.frame = CGRectMake(width*i, 0, width, height);
    }
    //设置偏移量
    self.scrollContentView.contentSize = CGSizeMake(width*index, 0);
    //设置接诊条件和医生简介(label)
    [self setLabel];
}
/**
 设置底部视图中的文字显示
 */
-(void)setLabel{
    UILabel *condition = [[UILabel alloc]init];
    [self.viewArrs[0] addSubview:condition];
    condition.textAlignment = NSTextAlignmentLeft;
    [condition mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.viewArrs[0]);
    }];
    //自动折行设置
    condition.lineBreakMode = NSLineBreakByWordWrapping;
    condition.numberOfLines = 0;
    self.coditionLabel = condition;
    
    UILabel *intro = [[UILabel alloc]init];
    [self.viewArrs[1] addSubview:intro];
    intro.textAlignment = NSTextAlignmentLeft;
    [intro mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.viewArrs[1]);
    }];
    //自动折行设置
    intro.lineBreakMode = NSLineBreakByWordWrapping;
    intro.numberOfLines = 0;
    self.introduceLabel = intro;
}

#pragma mark - scrollview代理方法

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat width = self.view.bounds.size.width;
    NSInteger index = self.scrollContentView.contentOffset.x/width;
    if (index == 0) {
        [UIView animateWithDuration:0.6 animations:^{
            [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.view);
                make.top.equalTo(self.leftLabel.mas_bottom);
            }];
        }];
    }else if (index == 1){
        [UIView animateWithDuration:0.6 animations:^{
            [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.view).mas_offset(self.view.bounds.size.width/2);
                make.top.equalTo(self.rightLabel.mas_bottom);
            }];
        }];
    }
}



+ (void)jhsShowSVSuccessTitle:(NSString *)content andTime:(float)delay{
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD setBackgroundColor:JHSMainColor];
    [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"%@",content]];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
    });
}

@end
